package com.example.park2212.park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.lib_base.entity.ParkBeanItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.databinding.ActivityParkEditBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/park/ParkEditActivity")
class ParkEditActivity : BaseActivity<ActivityParkEditBinding,ParkViewModel>() {
    @Autowired
    lateinit var bean:ParkBeanItem
    var status = ""
    var area = ""
    var type = ""
    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.headParkAdd.headerTitle.setText("车位编辑")
        binding.headParkAdd.headerBack.setOnClickListener {
            finish()
        }
        binding.headParkAdd.headerAdd.apply {
            setImageResource(R.drawable.ic_action_ok)
            setOnClickListener {
                val remark = binding.parkaddRemark.text.toString()
                val carNo = binding.parkaddCarNum.text.toString()
                val user = binding.parkaddUser.text.toString()
                val item = ParkBeanItem(bean.areaId,bean.areaName,
                bean.id,carNo,remark,bean.spaceNumber,status,bean.spaceType,bean.userId,user)
                viewModel.updatePark(bean.id!!,item)
            }
        }

        binding.parkaddCarNo.setText(bean.spaceNumber)
        binding.parkaddRemark.setText(bean.remarks)
        binding.parkaddUser.setText(bean.userName)
        binding.parkaddCarNum.setText(bean.licensePlate)

        if (bean.spaceStatus == "空闲"){
            binding.parkaddRg.check(R.id.rb_kx)
        }else if (bean.spaceStatus == "已占用"){
            binding.parkaddRg.check(R.id.rb_zy)
        }else{
            binding.parkaddRg.check(R.id.rb_wh)
        }

        val arr = mutableListOf<String>("A区停车场", "B区停车场", "C区停车场")
        if (!arr.contains(bean.areaName)){
            arr.add(bean.areaName)
        }

        val adapter = ArrayAdapter(this,android.R.layout.simple_spinner_item,arr)
        binding.parkaddArea.adapter = adapter
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddArea.setSelection(arr.indexOf(bean.areaName))

        binding.parkaddArea.onItemSelectedListener = object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                area = arr[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        val arrType = mutableListOf<String>("标准车位","VIP车位")
        if (!arrType.contains(bean.spaceType)){
            arrType.add(bean.spaceType)
        }

        val adapter2 = ArrayAdapter(this,android.R.layout.simple_spinner_item,arrType)
        binding.parkaddType.adapter = adapter2
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddType.setSelection(arrType.indexOf(bean.spaceType))

        binding.parkaddType.onItemSelectedListener = object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                type = arrType[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }

        binding.parkaddRg.setOnCheckedChangeListener { radioGroup, i ->
            if (i == R.id.rb_kx){
                status = "空闲"
            }else if (i == R.id.rb_zy){
                status = "已占用"
            }else{
                status = "维护中"
            }
        }

    }

    override fun initData() {
        viewModel.updateParkSuccess.observe(this){
            ToastUtils.showLong("修改成功")
            finish()
        }
    }

}